import {Component, Input, OnInit, ViewChild, Output, EventEmitter} from "@angular/core";
import {ProjectItemModel} from "../../model/project-item.model";
import {BookingHouseModalViewComponent} from "../../views/booking-house-modal-view/booking-house-modal-view.component";
import {ROUTE} from "../../service/route.service";
import {LoginModalViewComponent} from "../../views/login-modal-view/login-modal-view.component";
import {LocalStorage} from "../../app.local-storage";
import {UserModel} from "../../model/user.model";
import {USER} from "../../service/user.service";

@Component({
  selector: 'wf-list-item',
  templateUrl: './list-item.component.html',
  styleUrls: ['./list-item.component.scss']
})
export class ListItemComponent implements OnInit {

  @Input('project') item: ProjectItemModel = new ProjectItemModel();
  showModal: boolean = false;
  @ViewChild(BookingHouseModalViewComponent) bookingModal:any;
  @ViewChild(LoginModalViewComponent) loginModal:any;
  apartmentId:number;
  showLoginModal: boolean = false;
  user:UserModel;
  constructor(private route: ROUTE,
              private localeStorage:LocalStorage,
              private USER: USER) {
    this.user = this.USER.get();

  }

  ngOnInit() {

  }

  mouseOver() {
    if (this.item) {
      this.item.hover = true;
    }
  }

  mouseLeave() {
    if (this.item) {
      this.item.hover = false;
    }
  }

  showBookingModal(item:any) {
    this.apartmentId = item.id;
    this.bookingModal.showModal();
  }

  closeModal(result: Object) {
    this.bookingModal.onHidden();
  }
  goToRentingView(item:any){
      this.route.gotoRenting(item);
  }

  closeLogin(result:Object){
    if(result instanceof UserModel){
      this.user=result;
      this.USER.update(result);
    }
    this.showLoginModal=false;
  }
  // ensureLogin() {
  //   if(this.localeStorage.getObject('user').value=={}){
  //     this.showLoginModal=true;
  //     return false;
  //   }
  //   return true;
  // }
  onItemClick(item:any){
    this.route.goProjectDetailView(item);
  }
}
